package com.ruoyi.web.controller.h5;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.dto.FavoriteAddDTO;
import com.ruoyi.system.dto.FavoriteQueryDTO;
import com.ruoyi.system.service.IFavoriteService;
import com.ruoyi.system.vo.FavoriteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * H5收藏控制器
 * 
 * @author ruoyi
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/h5/favorite")
@Api(tags = "H5收藏管理")
public class FavoriteController extends BaseController {

    @Resource
    private IFavoriteService favoriteService;

    /**
     * 添加收藏
     */
    @PostMapping("/add")
    @ApiOperation("添加收藏")
    public AjaxResult addFavorite(@Validated @RequestBody FavoriteAddDTO favoriteAddDTO) {
        // 这里需要从当前登录用户获取userId，暂时使用固定值
        Long userId = SecurityUtils.getH5LoginUser().getUserId();
        boolean result = favoriteService.addFavorite(userId, favoriteAddDTO);
        return AjaxResult.success(result);
    }

    /**
     * 取消收藏
     */
    @DeleteMapping("/remove")
    @ApiOperation("取消收藏")
    public AjaxResult removeFavorite(@RequestParam String businessType, @RequestParam Long businessId) {
        Long userId = SecurityUtils.getH5LoginUser().getUserId();
        boolean result = favoriteService.removeFavorite(userId, businessType, businessId);
        return AjaxResult.success(result);
    }

    /**
     * 查询用户收藏列表
     */
    @PostMapping("/list")
    @ApiOperation("查询用户收藏列表")
    public AjaxResult getUserFavorites(@RequestBody FavoriteQueryDTO queryDTO) {
        // 设置当前用户ID
        queryDTO.setUserId(SecurityUtils.getH5LoginUser().getUserId());
        List<FavoriteVO> favorites = favoriteService.getUserFavorites(queryDTO);
        return AjaxResult.success(favorites);
    }

    /**
     * 检查是否已收藏
     */
    @GetMapping("/check")
    @ApiOperation("检查是否已收藏")
    public AjaxResult checkFavorite(@RequestParam String businessType, @RequestParam Long businessId) {
        Long userId = SecurityUtils.getH5LoginUser().getUserId();
        boolean isFavorited = favoriteService.isFavorited(userId, businessType, businessId);
        return AjaxResult.success(isFavorited);
    }

    /**
     * 获取收藏数量
     */
    @GetMapping("/count")
    @ApiOperation("获取收藏数量")
    public AjaxResult getFavoriteCount(@RequestParam String businessType, @RequestParam Long businessId) {
        int count = favoriteService.getFavoriteCount(businessType, businessId);
        return AjaxResult.success(count);
    }


}
